package com.green.common.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.MessageDigest;
/**
 * 嘉盈短信新接口
 * @author Administrator
 *
 */
public class HttpTextUtil {


	static final String _uc="7136";// 帐号
	static final String _pwd="Shdc999luck"; //密码
	
	static final String _host = "http://kltx.sms10000.com.cn/sdk/SMS?";

	public static String get_pwd() {
		return MD5Encode(_pwd);
	}

	/**
	 * 发送短信
	 * 
	 * @param mobiles
	 *            String 接收号码
	 * @param cont
	 *            String 短信内容
	 * @param msgid
	 *            String 短信ID
	 * @return String
	 */
	public static String sendSMS(String mobiles, String cont, String msgid) {
		String re = "";
		try {
			cont = URLEncoder.encode(cont, "GBK"); // 短信内容需要编码
			String sendUrl = _host + "cmd=send&uid=" + _uc + "&psw="
					+ get_pwd() + "&mobiles=" + mobiles + "&msgid="
					+ msgid + "&msg=" + cont + " ";
			re = submit(sendUrl);
	
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return re;
	}

	/**
	 * 接收短信
	 * 
	 * @return String
	 */
	public String getMO() {
		String re = "";
		try {
			String moUrl = _host + "cmd=getmo&uid=" + _uc + "&psw="
					+ get_pwd() + "";
			re = submit(moUrl);
		} catch (Exception e) {
			
			e.printStackTrace();

		}
		return re;
	}

	/**
	 * 取发送状态
	 * 
	 * @return String
	 */
	public static String getStatus() {
		String re = "";
		String getstatusUrl = _host + "cmd=getstatus&uid=" + _uc + "&psw="
				+ get_pwd() + "";
		re = submit(getstatusUrl);
		return re;
	}

	/**
	 * GET提交
	 * 
	 * @param url
	 *            String
	 * @return String
	 */
	private static String submit(String url) {
		String re = "";
		int byteMax = 50;
		try {
			URL myurl = new URL(url);
			URLConnection urlcon = myurl.openConnection();
			urlcon.setUseCaches(false);
			urlcon.connect();
			InputStream inputstream = urlcon.getInputStream();
			byte[] buff = new byte[byteMax];
			ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
			int ins;
			while ((ins = inputstream.read(buff)) >= 0) {
				bytearrayoutputstream.write(buff, 0, ins);
			}
			re = new String(bytearrayoutputstream.toByteArray());
			re.trim();
		} catch (MalformedURLException ex) {
			
		} catch (IOException ex) {
			/** @todo Handle this exception */
		}

		return re;
	}

	/**
	 * MD5加密
	 * 
	 * @param origin
	 * @return
	 */
	private static String MD5Encode(String origin) {
		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = byteArrayToHexString(md.digest(resultString
					.getBytes()));
		} catch (Exception ex) {

		}
		return resultString;
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0) {
			n = 256 + n;
		}
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}

	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	/**
	 * 测试发送
	 * @param args
	 */
	public static void main(String[] args) {
		
		/*String returnStr=sendSMS("18676665381",
				"大家好，msgId验证！",
				"12345678");
		
		System.out.println(returnStr);
		*/
		String aa=getStatus();
		
		System.out.println("###############");
		if(null!=aa && !"".equals(aa)){
			String[] pp=aa.split("\n");
			for(int i=0;i<pp.length;i++){
				System.out.println(pp[i]);
			}
		}
		
	}
}
